<template>
  <div class="WorkingSchedule" v-loading="loading">
    <el-form ref="form" :model="form" label-position="top" class="myform">
      <div>
        <el-form-item label="操作名称" class="myform-item">
          <el-input
            v-model="form.rolename"
            placeholder="请输入操作名称"
            @keyup.enter.native="onSearch"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="操作状态" class="myform-item">
          <el-select v-model="form.status" placeholder="请选择" clearable>
            <el-option
              v-for="item in optionsTwo"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </div>
      <div>
        <el-form-item style="margin-top: 50px">
          <el-button type="primary" @click="onSearch">查询</el-button>
          <el-button type="primary" plain @click="onAddModel"
            >新增操作</el-button
          >
          <!-- <el-button type="primary">上传文件</el-button> -->
        </el-form-item>
      </div>
    </el-form>
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column label="操作名称" align="center" prop="rolename">
      </el-table-column>
      <el-table-column label="标识" align="center" prop="rolekey">
      </el-table-column>
      <el-table-column label="描述" align="center" prop="description">
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="{ row }">{{
          row.status == 0 ? "启用" : "禁用"
        }}</template>
      </el-table-column>

      <el-table-column label="创建时间" align="center" prop="createtime">
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createuser">
      </el-table-column>
      <el-table-column label="排序" align="center" prop="listorder">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <!-- <el-button type="text" @click="onLook(row.id)">查看</el-button> -->
          <el-button type="text" @click="onEdit(row)">编辑</el-button>
          <el-button type="text" @click="onDelete(row.id)" style="color: red"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :form="form"
      :total="total"
      @paginationPostOrderTaskList="postAdminUserOperationlist"
    />
    <el-dialog title="操作" :visible.sync="dialogVisible" width="30%" :close-on-click-modal="false">
      <el-form
        :model="formChild"
        label-position="top"
        :rules="rulesChild"
        ref="ruleFormChild"
      >
        <el-form-item label="操作名称" prop="rolename">
          <el-input
            v-model="formChild.rolename"
            placeholder="请输入操作名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="操作标识" prop="rolekey">
          <el-input
            v-model="formChild.rolekey"
            placeholder="请输入操作标识"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="操作状态" prop="status">
          <el-select v-model="formChild.status" placeholder="请选择">
            <el-option
              v-for="item in optionsTwo"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="操作排序（数字越大排序约靠前）" prop="listorder">
          <el-input
            v-model="formChild.listorder"
            placeholder="操作排序（数字越大排序约靠前）"
            type="number"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="操作描述" prop="description">
          <el-input
            v-model="formChild.description"
            placeholder="请输入操作描述"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="onConfirmChild('ruleFormChild')"
            :loading="loading"
            >保存</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  postAdminUserOperationlist,
  postAdminUserDeloperation,
  postAdminUserEditoperation,
} from "@/api";
import pagination from "@/components/pagination.vue";
export default {
  components: { pagination },
  data() {
    return {
      loading: false,
      dialogVisible: false,
      form: {
        status: "",
        rolename: "",
        page: 1,
        size: 10,
      },
      tableData: [],
      total: 1,
      optionsTwo: [
        {
          label: "启用",
          value: "0",
        },
        {
          label: "禁用",
          value: "1",
        },
      ],
      formChild: {
        rolename: "",
        rolekey: "",
        status: "",
        listorder: "",
        description: "",
      },
      rulesChild: {
        rolename: [
          { required: true, message: "请输入操作名称", trigger: "blur" },
        ],
        unique: [
          { required: true, message: "请输入操作标识", trigger: "blur" },
        ],
        status: [{ required: true, message: "请选择状态", trigger: "blur" }],
        listorder: [
          { required: true, message: "请输入操作排序", trigger: "blur" },
        ],
        description: [
          { required: true, message: "请输入操作描述", trigger: "blur" },
        ],
      },
    };
  },
  activated() {
    let form = this.form;
    this.postAdminUserOperationlist(form);
  },
  methods: {
    // 查看
    // onLook(id) {
    //   this.$router.push({ name: "SettingModelDetail", query: { id } });
    // },
    onConfirmChild(e) {
      let that = this;
      that.loading = true;
      this.$refs[e].validate(async (valid) => {
        if (valid) {
          await postAdminUserEditoperation(that.formChild).then((res) => {
            let form = that.form;
            that.postAdminUserOperationlist(form);
            that.dialogVisible = false;
            if (that.formChild.id) {
              that.$message.success("操作编辑成功");
            } else {
              that.$message.success("操作新增成功");
            }
          });
          that.loading = false;
        } else {
          that.loading = false;
          return false;
        }
      });
    },
    // 新增模块
    onAddModel() {
      this.reset();
      this.dialogVisible = true;
    },
    // 删除
    onDelete(id) {
      let obj = {
        id,
      };
      this.$confirm("确定删除该模块?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          postAdminUserDeloperation(obj).then((res) => {
            let form = this.form;
            this.postAdminUserOperationlist(form);
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    reset() {
      this.formChild = {
        rolename: "",
        rolekey: "",
        status: "",
        listorder: "",
        description: "",
      };
    },
    // 编辑
    onEdit(e) {
      this.formChild = {
        id: e.id,
        rolename: e.rolename,
        rolekey: e.rolekey,
        status: e.status,
        listorder: e.listorder,
        description: e.description,
      };
      this.dialogVisible = true;
    },
    // qc列表
    async postAdminUserOperationlist(data) {
      this.loading = true;
      this.form = data;
      let form = JSON.parse(JSON.stringify(this.form));
      // if (form.qc_time) {
      //   form.qc_time = `${form.qc_time[0]}/${form.qc_time[1]}`;
      // }
      postAdminUserOperationlist(form)
        .then((res) => {
          this.loading = false;
          console.log("qc列表", res);
          this.tableData = res.data;
          this.total = Number(res.total);
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 搜索
    onSearch() {
      this.form.page = 1;
      this.form.size = 10;
      this.postAdminUserOperationlist(this.form);
    },
  },
};
</script>

<style lang="scss" scoped>
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
.WorkingSchedule {
  background: #ffff;
  padding: 20px;
  border-radius: 2px;
  @include boxShadow;
}
</style>
